package com.websuites.core.response;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.websuites.core.Constant;
import com.websuites.utils.JsonUtil;

@SuppressWarnings("unchecked")
public class Results 
{
	private HashMap<String,IResult> mapRes = new HashMap<String,IResult>();
	
	public synchronized void putResult(String sBex, IResult rs)
	{
		mapRes.put(sBex, rs);
	}
	
	public IResult getResult(String sBex)
	{
		return (IResult)mapRes.get(sBex);
	}

	public IResult getResult()
	{
		Iterator itRes = mapRes.entrySet().iterator();
		while (itRes.hasNext())
		{
			Map.Entry element = (Map.Entry)itRes.next();
			IResult rs = (IResult)element.getValue();
			if (!rs.isSuccessful())
			{
				return rs;
			}
		}
		
		IResult rs = new Result();
		return rs;
	}

	public String toXml()
	{
		StringBuffer sXML = new StringBuffer();
		sXML.append("<?xml version=\"1.0\" encoding=\"GBK\"?><response>");

		Iterator itRes = mapRes.entrySet().iterator();
		while (itRes.hasNext())
		{
			Map.Entry element = (Map.Entry)itRes.next();
			String sBex = element.getKey().toString(); 
			IResult rs = (IResult)element.getValue();
				
			sXML.append("<" + sBex + ">");
			sXML.append(rs.toXml());
			sXML.append("</" + sBex + ">");
		}
		sXML.append("</response>");

		return sXML.toString();
	}
	
	public String toJson()
	{
		StringBuffer sJSON = new StringBuffer();		
		JsonUtil.beginMap(sJSON);
		
		boolean bFirstBex = true;
		Iterator itRes = mapRes.entrySet().iterator();
		while (itRes.hasNext())
		{
			Map.Entry element = (Map.Entry)itRes.next();
			String sBex = element.getKey().toString(); 
			IResult rs = (IResult)element.getValue();
			
			if (bFirstBex)
			{
				JsonUtil.makePair(sJSON, sBex, rs.toJson());
				bFirstBex = false;
			}
			else
			{
				JsonUtil.makePair(sJSON, sBex, rs.toJson(), ",");
			}
		}
		JsonUtil.endMap(sJSON);

		return sJSON.toString();
	}

	public String toXml2()
	{
		StringBuffer sXML = new StringBuffer();
		sXML.append("<?xml version=\"1.0\" encoding=\"GBK\"?><response>");

		sXML.append("<bex>");
		sXML.append("<flag>");
		sXML.append(Constant.WS_ERROR_SUCCESS);
		sXML.append("</flag><prompt>OK</prompt><length>1</length><data id=\"1\">");
		
		StringBuffer sRecord = new StringBuffer();

		int iLen = 0;
		Iterator itRes = mapRes.entrySet().iterator();
		while (itRes.hasNext())
		{
			Map.Entry element = (Map.Entry)itRes.next();
			String sBex = element.getKey().toString(); 
			IResult rs = (IResult)element.getValue();
			
			if (rs.getErrorCode() == Constant.WS_ERROR_FAILURE)
			{		
				StringBuffer sFail = new StringBuffer();
				sFail.append("<bex>");
				sFail.append("<flag>");
				sFail.append(rs.getErrorCode());
				sFail.append("</flag><prompt>");
				sFail.append(sBex);
				sFail.append(":");
				sFail.append(rs.getErrorMessage());
				sFail.append("</prompt>");
				sFail.append("</bex>");
				
				return sFail.toString();
			}
			else
			{
				iLen += rs.getListLen();
				sRecord.append(rs.getListXML());
			}
		}
		sXML.append("<length>");
		sXML.append(iLen);
		sXML.append("</length>");
		sXML.append("<lengths>");
		sXML.append(iLen);
		sXML.append("</lengths>");

		sXML.append("<records>");
		sXML.append(sRecord);
		sXML.append("</records>");
		sXML.append("</data>");	
		sXML.append("</bex>");	

		sXML.append("</response>");

		return sXML.toString();
	}
	
	public String toJson2()
	{
		StringBuffer sRecords = new StringBuffer();
		JsonUtil.beginArray(sRecords);
		
		int iLen = 0;
		Iterator itRes = mapRes.entrySet().iterator();
		while (itRes.hasNext())
		{
			Map.Entry element = (Map.Entry)itRes.next();
			String sBex = element.getKey().toString(); 
			IResult rs = (IResult)element.getValue();
			
			if (rs.getErrorCode() == Constant.WS_ERROR_FAILURE)
			{		
				StringBuffer sFail = new StringBuffer();		
				JsonUtil.beginMap(sFail);
				JsonUtil.makePair(sFail, "flag", rs.getErrorCode());
				JsonUtil.makePair(sFail, "prompt", sBex, ",");
				JsonUtil.makePair(sFail, "prompt", sBex, ",");
				JsonUtil.endMap(sFail);
				
				StringBuffer sJSON = new StringBuffer();		
				JsonUtil.beginMap(sJSON);
				JsonUtil.makePair(sJSON, "bex", sFail);
				JsonUtil.endMap(sJSON);
				
				return sJSON.toString();
			}
			else
			{
				if (iLen > 0)
				{
					JsonUtil.makeArray(sRecords, rs.getListJson(), ",");
				}
				else
				{
					JsonUtil.makeArray(sRecords, rs.getListJson());
				}
				iLen += rs.getListLen();
			}
		}
		JsonUtil.endArray(sRecords);
		
		StringBuffer sData = new StringBuffer();
		JsonUtil.beginMap(sData);
		JsonUtil.makePair(sData, "length", iLen);
		JsonUtil.makePair(sData, "lengths", iLen, ",");
		JsonUtil.makePair(sData, "records", sRecords);
		JsonUtil.endMap(sData);
		
		StringBuffer sMultiData = new StringBuffer();
		JsonUtil.beginArray(sMultiData);
		JsonUtil.makeArray(sMultiData, sData);
		JsonUtil.endArray(sMultiData);
			
		StringBuffer sResult = new StringBuffer();		
		JsonUtil.beginMap(sResult);
		JsonUtil.makePair(sResult, "flag", Constant.WS_ERROR_SUCCESS);
		JsonUtil.makePair(sResult, "prompt", "OK", ",");
		JsonUtil.makePair(sResult, "length", 1, ",");
		JsonUtil.makePair(sResult, "flag", Constant.WS_ERROR_SUCCESS, ",");
		JsonUtil.makePair(sResult, "data", sMultiData, ",");	
		JsonUtil.endMap(sResult);
		
		StringBuffer sJSON = new StringBuffer();		
		JsonUtil.beginMap(sJSON);
		JsonUtil.makePair(sJSON, "bex", sResult);
		JsonUtil.endMap(sJSON);
		
		return sJSON.toString();
	}

}
